/**
 * @author Helio
 */
if(!SALE.dao) SALE.dao = {}
if(!SALE.dao.local) SALE.dao.local = {}
if(!SALE.dao.local.BillCodeSupportDao) SALE.dao.local.BillCodeSupportDao = function(){
	var billCodeDoc;
}

SALE.dao.local.BillCodeSupportDao.prototype = {	
		
		initDoc: function() {
			if(!this.billCodeDoc) {
				this.billCodeDoc = LocalXMLFileUtil.getBillCodeDoc();
			}
			return this.billCodeDoc;
		},
		
		saveDoc : function() {
			LocalXMLFileUtil.saveXML(this.billCodeDoc,LocalXMLFileUtil.getBillCodeFile());
		},
		
		createBillCode : function(type) {
			
			var codeNode = this.searchBillCodeByID(type);
			var date = new Date();
			
			date = date.format('yyyyMMdd');
			
			if(!codeNode) {
				codeNode = this.billCodeDoc.createElement("code");				
				codeNode.setAttribute('type', type);
				codeNode.setAttribute('date', date);
				codeNode.setAttribute('number', "000");
				
				var root = this.billCodeDoc.documentElement;
				root.appendChild(codeNode);
			}
			
			if(date != codeNode.getAttribute('date')) {
				codeNode.setAttribute('date', date);
				codeNode.setAttribute('number', "000");
			}
			
			var number = codeNode.getAttribute('number');
			number = String.leftPad(parseFloat(number) + 1, 3, "0" );
			codeNode.setAttribute('number', number);
						
			this.saveDoc();
			
			var code = codeNode.getAttribute('type') + "-" + codeNode.getAttribute('date') + "-" + codeNode.getAttribute('number');
			
			return code;
		},
				
		searchBillCodeByID : function(type) {
			var root = this.billCodeDoc.documentElement;
			var objArray = Ext.query("/code[type=" + type + "]", root);
			var result;
			
			if(objArray.length == 1) {
				result = objArray[0];
			}
			
			return result;
		}
}
